<script language="JavaScript" type="text/JavaScript">
	var cmd = "";
	var location_id = "";
	var exch_name = "";
	var province_id = "";
	
	function getProvince(){
	    var session = Math.random();
	    new Ajax.Request(
	    "ajax_getprov.php?session=" + session ,			// url
	    { asynchronous: true,			// default is true
	        method: "get",				// default is post
	        onSuccess: function(request) {
	           rtnProv(request.responseText);
	        }
	    });	
	    waiting.innerHTML = '<img src="images/loading1.gif"/> <span class="required">process...</span>';	
	}
	
	function rtnProv(txt){		
		
		var html = '<fieldset><legend class="txt_module_head">เพิ่มข้อมูล</legend>'+
		'<table width="100%" border="0" cellspacing="3" cellpadding="2">'+
		  '<tr>'+
		    '<td width="30%" align="right" class="line_dash" ><span class="txt_module">Location</span> : </td>'+
		    '<td class="line_dash">'+				
		        '<input name="location_id" id="location_id" type="text" class="textBox_form" />  </td>'+
		  '</tr>'+
		  '<tr>'+
		    '<td width="30%" align="right" class="line_dash"><span class="txt_module">ชื่อชุมสาย</span> : </td>'+
		    '<td class="line_dash">'+		
		        '<input name="exch_name" id="exch_name" type="text" class="textBox_form" />    </td>'+
		  '</tr>'+
		  '<tr>'+
		    '<td width="30%" align="right" class="line_dash"><span class="txt_module">จังหวัด</span> : </td>'+
		    '<td class="line_dash">'+		
		        '<select name="ls_province" id="ls_province" class="combo">' + txt + '</select></td>'+
		  '</tr>'+
		  '<tr>'+
		    '<td align="right" >&nbsp;&nbsp;    </td>';		    
		    if(cmd == "add"){
		    	html += '<td ><img src="images/ok-32.png" alt="" width="32" height="19" onclick="cmdExchName(\'0\',\'add\');" /></td>';
		    }else{
		    	html += '<td ><img src="images/ok-32.png" alt="" width="32" height="19" onclick="cmdExchName(\'0\',\'edit\');" /></td>';
		    }
		    
		  html += '</tr>'+
		'</table>'+
		'</fieldset>';
		
		exchname_add.innerHTML = html;				
		//alert(location_id);
		if(cmd == "edit"){		
			document.getElementById("location_id").disabled = true;
			document.getElementById("location_id").value = location_id;
			document.getElementById("exch_name").value = exch_name;									
			var ls_prov = document.getElementById("ls_province");
			//alert(ls_prov.length);
			for(var i = 0; i < ls_prov.length; i++){
				if(province_id == ls_prov[i].value){				
					ls_prov[i].selected = true;
					break;
				}
			}
		}		
		waiting.innerHTML = '';
	}
	
	function actionCmd(command){
		document.getElementById("view_header").style.visibility='visible';
		cmd = command;
		getProvince();		
	}
	
	function actionEdit(llc, len, lpv){
		location_id = llc;
		exch_name = len;
		province_id = lpv;
		actionCmd("edit");
				
	}
	
	function cmdExchName(code, cmd){
		//alert(cmd);
		
		if(cmd == "add"){			
			location_id = document.getElementById("location_id").value;
			exch_name = document.getElementById("exch_name").value;									
			var ls_prov = document.getElementById("ls_province");
			//alert(ls_prov.length);
			for(var i = 0; i < ls_prov.length; i++){
				if(ls_prov[i].selected == true){				
					province_id = ls_prov[i].value
					break;
				}
			}
					
		}else if(cmd == "del"){
	    	var conf = confirm("ท่านต้องการจะ ลบ ข้อมูลใช่หรือไม่");
	    	if(conf == false){    
	    		return;
	    	}			
			location_id = code;
		}else if(cmd == "edit"){			
			location_id = document.getElementById("location_id").value;
			exch_name = document.getElementById("exch_name").value;									
			var ls_prov = document.getElementById("ls_province");
			//alert(ls_prov.length);
			for(var i = 0; i < ls_prov.length; i++){
				if(ls_prov[i].selected == true){				
					province_id = ls_prov[i].value
					break;
				}
			}
		}
		
        var session = Math.random();
        new Ajax.Request(
        "ajax_cmexchname.php?session=" + session +'&location_id=' + location_id + "&exch_name=" + exch_name + "&province_id=" + province_id + "&action=" + cmd,			// url
        { asynchronous: true,			// default is true
            method: "get",				// default is post
            onSuccess: function(request) {
                rtn(request.responseText);
            }
        });	
		
		waiting.innerHTML = '<img src="images/loading1.gif"/> <span class="required">process...</span>';        		
	}
	
	function rtn(txt){
		view.innerHTML = txt;
		waiting.innerHTML = "";
	}
	
</script>
<div id="content">
	<div class="post">		
		<h2>แสดงข้อมูลเกี่ยวกับ ชุมสาย</h2>
		<div class="entry">
			<div id="view">{VIEW_EXCHNAME}</div>
		</div>
	</div>
	<div class="post"><div id="waiting"></div>					
		<div style="visibility:hidden" id="view_header"><h2>จัดการข้อมูลเกี่ยวกับ ชุมสาย</h2></div>
		<div class="entry">							
			<div id="exchname_add"></div>
		</div>
	</div>
</div>